package com.neo.controller.area;

import cn.shuibo.annotation.Decrypt;
import cn.shuibo.annotation.Encrypt;
import com.neo.common.base.Base2Controller;
import com.neo.dto.AreaDto;
import com.neo.dto.ProvinceDto;
import com.neo.service.AreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/area")
@Api(value = "行政区划", description = "行政区划", tags = "area")
public class AreaController extends Base2Controller {
    @Autowired
    private AreaService areaService;


    @Decrypt
    @Encrypt
    @GetMapping(value = "/getAreaById")
    @ApiOperation(value = "根据省份编号获取所有城市", notes = "/getCityById", response = AreaDto.class)
    @ResponseBody
    public Map<String, Object> getAreaById(@ApiParam("用户编号") @RequestParam(value = "userId") String userId,
                                           @ApiParam("父级编号(查询省份信息时，不需要填，如果查城市那么输入省级编号，以此类推)") @RequestParam(value = "parentId", required = false, defaultValue = "0") String parentId,
                                           @ApiParam("token") @RequestParam(value = "token") String token) {
        if (isEnabled(userId, token)) {
            return tokenFail();
        }
        return sucess(areaService.getAreaById(parentId));
    }

    @Decrypt
    @Encrypt
    @GetMapping(value = "/getAreaByLike")
    @ApiOperation(value = "根据关键字模糊查询所有地区", notes = "/getAreaByLike", response = ProvinceDto.class)
    @ResponseBody
    public Map<String, Object> getAreaByLike(@ApiParam("用户编号") @RequestParam(value = "userId") String userId,
                                             @ApiParam("关键字)") @RequestParam(value = "keyWord") String keyWord,
                                             @ApiParam("token") @RequestParam(value = "token") String token) {
        if (isEnabled(userId, token)) {
            return tokenFail();
        }
        return sucess(areaService.getAreaByLike(keyWord));
    }

    @Decrypt
    @Encrypt
    @PostMapping(value = "/updateArea")
    @ApiOperation(value = "新增或修改地区信息", notes = "/updateArea")
    @ResponseBody
    public Map<String, Object> updateArea(@ApiParam("用户编号") @RequestParam(value = "userId") String userId,
                                          @ApiParam("名称)") @RequestParam(value = "name") String name,
                                          @ApiParam("行政区划码)") @RequestParam(value = "code", required = false) String code,
                                          @ApiParam("操作类型（0 新增，1修改）)") @RequestParam(value = "type", required = false, defaultValue = "0") Integer type,
                                          @ApiParam("地区编号，当操作类型为新增时时不需要填写)") @RequestParam(value = "id", required = false) Integer id,
                                          @ApiParam("token") @RequestParam(value = "token") String token) {
        if (isEnabled(userId, token)) {
            return tokenFail();
        }
        int i = areaService.updateArea( name, code, type, id);
        if (i > 0) {
            return sucess();
        }
        return fail();
    }

}
